package com.airsystem.pos.bengkel.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import com.airsystem.pos.bengkel.controller.MasterSparepartController;
import com.airsystem.pos.bengkel.controller.MenuController;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterSparepart extends MasterSparepartController {

	public MasterSparepart(MenuController menuController) {
		super();

		this.menuController = menuController;
		initComponents();
		setup();
	}

	public void initComponents() {
		setTitle("Data Sparepart");
		setFrameIcon(new ImageIcon(getClass().getResource("/images/sparepartx.png")));
		setIconifiable(true);
		setClosable(true);

		baseLayout.columnWidths = new int[] { 0, 0 };
		baseLayout.rowHeights = new int[] { 0, 0 };
		baseLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		baseLayout.rowWeights = new double[] { 1.0, 1.0 };
		getContentPane().setLayout(baseLayout);

		panelLayout.columnWidths = new int[] { 100, 0, 0 };
		panelLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		panelLayout.columnWeights = new double[] { 0.0, 1.0, 0.0 };
		panelLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
		basePanel.setLayout(panelLayout);

		merksparepartLabel.setFont(new Font("Verdana", 0, 12));
		merksparepartLabel.setText("Merk Sparepart");

		submerksparepartLabel.setFont(new Font("Verdana", 0, 12));
		submerksparepartLabel.setText("Sub Merk Sparepart");

		jenisLabel.setFont(new Font("Verdana", 0, 12));
		jenisLabel.setText("Jenis Sparepart");

		merkkendaraanLabel.setFont(new Font("Verdana", 0, 12));
		merkkendaraanLabel.setText("Merk Kendaraan");

		submerkkendaraanLabel.setFont(new Font("Verdana", 0, 12));
		submerkkendaraanLabel.setText("Sub Merk Kendaraan");

		stokLabel.setFont(new Font("Verdana", 0, 12));
		stokLabel.setText("Stok");

		hargaLabel.setFont(new Font("Verdana", 0, 12));
		hargaLabel.setText("Harga Satuan");

		merksparepartField.setFont(new Font("Verdana", 0, 12));
		merksparepartField.setColumns(15);
		merksparepartField.setDocument(new TextUtil(30, TextUtil.CAPITAL));

		submerksparepartField.setFont(new Font("Verdana", 0, 12));
		submerksparepartField.setColumns(15);
		submerksparepartField.setDocument(new TextUtil(30, TextUtil.ALPHACAPITAL));

		jenisField.setFont(new Font("Verdana", 0, 12));
		jenisField.setColumns(15);

		merkkendaraanField.setFont(new Font("Verdana", 0, 12));
		merkkendaraanField.setColumns(15);

		submerkkendaraanField.setFont(new Font("Verdana", 0, 12));
		submerkkendaraanField.setColumns(15);

		stokField.setFont(new Font("Verdana", 0, 12));
		stokField.setColumns(15);
		stokField.setDocument(new TextUtil(3, TextUtil.NUMBER));

		hargaField.setFont(new Font("Verdana", 0, 12));
		hargaField.setColumns(15);
		hargaField.setDocument(new TextUtil(17, TextUtil.NUMBER));

		idField.setFont(new Font("Verdana", 0, 12));
		idField.setColumns(15);

		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 30));

		searchButton.setIcon(new ImageIcon(getClass().getResource("/images/search.png")));
		searchButton.setFont(new Font("Verdana", 1, 12));
		searchButton.setText("CARI");

		sparepartButton.setIcon(new ImageIcon(getClass().getResource("/images/select.png")));
		sparepartButton.setFont(new Font("Verdana", 1, 12));
		sparepartButton.setText("PILIH");

		vehicleButton.setIcon(new ImageIcon(getClass().getResource("/images/select.png")));
		vehicleButton.setFont(new Font("Verdana", 1, 12));
		vehicleButton.setText("PILIH");

		saveButton.setIcon(new ImageIcon(getClass().getResource("/images/save.png")));
		saveButton.setFont(new Font("Verdana", 1, 12));
		saveButton.setText("SIMPAN");
		buttonPanel.add(saveButton);

		updateButton.setIcon(new ImageIcon(getClass().getResource("/images/update.png")));
		updateButton.setFont(new Font("Verdana", 1, 12));
		updateButton.setText("UBAH");
		buttonPanel.add(updateButton);

		deleteButton.setIcon(new ImageIcon(getClass().getResource("/images/delete.png")));
		deleteButton.setFont(new Font("Verdana", 1, 12));
		deleteButton.setText("HAPUS");
		buttonPanel.add(deleteButton);

		cancelButton.setIcon(new ImageIcon(getClass().getResource("/images/cancel.png")));
		cancelButton.setFont(new Font("Verdana", 1, 12));
		cancelButton.setText("BATAL");
		buttonPanel.add(cancelButton);

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});

		sparepartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				sparepartButtonActionPerformed(evt);
			}
		});

		vehicleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				vehicleButtonActionPerformed(evt);
			}
		});

		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});

		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		basePanel.add(merksparepartLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 15, 15, 15), 0, 0));
		basePanel.add(submerksparepartLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(jenisLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(merkkendaraanLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(submerkkendaraanLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(stokLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(hargaLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 15), 0, 0));
		basePanel.add(merksparepartField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 0, 15, 0), 0, 0));
		basePanel.add(submerksparepartField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(jenisField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(merkkendaraanField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(submerkkendaraanField, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(stokField, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(hargaField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 15, 0), 0, 0));
		basePanel.add(searchButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 15, 15, 30), 0, 0));
		basePanel.add(sparepartButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 30), 0, 0));
		basePanel.add(vehicleButton, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 15, 30), 0, 0));
		basePanel.add(idField, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 15, 15, 30), 0, 0));

		getContentPane().add(basePanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(buttonPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 550) / 2, (screenSize.height - 550) / 2, 550, 400);
	}

	private void searchButtonActionPerformed(ActionEvent evt) {
		search();
	}

	private void sparepartButtonActionPerformed(ActionEvent evt) {
		choice = Choice.JENIS_SPAREPART;
		get();
	}

	private void vehicleButtonActionPerformed(ActionEvent evt) {
		choice = Choice.JENIS_KENDARAAN;
		get();
	}

	private void saveButtonActionPerformed(ActionEvent evt) {
		save();
	}

	private void updateButtonActionPerformed(ActionEvent evt) {
		update();
	}

	private void deleteButtonActionPerformed(ActionEvent evt) {
		delete();
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		cancel();
	}
}